package cn.xiongxf.system.controller;

import cn.xiongxf.basic.util.AjaxResult;
import cn.xiongxf.basic.util.PageList;
import cn.xiongxf.system.domain.SystemDictionaryType;
import cn.xiongxf.system.query.SystemDictionaryTypeQuery;
import cn.xiongxf.system.service.SystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemDictionaryType")
@Api(value = "数据字典明细表API",description = "数据字典明细表的CRUD操作") // 描述Controller
public class SystemDictionaryTypeController {
    @Autowired
    private SystemDictionaryTypeService systemDictionaryTypeService;

    /**
     * 接口：查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有") // 描述方法
    public List<SystemDictionaryType> findAll(){
        return systemDictionaryTypeService.findAll();
    }

    /**
     * 接口：查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public SystemDictionaryType findById(@PathVariable Long id){
        return systemDictionaryTypeService.findById(id);
    }

    /**
     * 接口：分页查询
     * @param systemDictionaryTypeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<SystemDictionaryType> queryPage(@RequestBody SystemDictionaryTypeQuery systemDictionaryTypeQuery){
        return systemDictionaryTypeService.queryPageList(systemDictionaryTypeQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult del(@PathVariable Long id){
        try {
            systemDictionaryTypeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try{
            systemDictionaryTypeService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：新增/修改
     * @param systemDictionaryType
     * @return
     */
    @PutMapping
    @ApiOperation(value = "新增/修改")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType systemDictionaryType){
        try {
            if (systemDictionaryType.getId() == null){
                systemDictionaryTypeService.add(systemDictionaryType);
            }else {
                systemDictionaryTypeService.update(systemDictionaryType);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

}
